<?php
namespace app\admin\controller;

use app\common\controller\AdminBaseController;
use app\common\model\AdminAccessModel;
use app\common\model\AdminModel;
use app\common\model\DeptModel;
use app\common\traits\adminTraits;
use app\common\traits\selectPageTraits;

class AdminController extends AdminBaseController
{
    /**
     * 追加查询条件
     * @var array
     */
    protected $appendQuery = [];
    /**
     * 需要鉴权的方法,需要登录
     * @var array
     */
    protected $needRight = [];

    use adminTraits;
    use selectPageTraits;

    /**
     * 初始化方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new AdminModel();
    }


    /**
     * 搜索器配置
     */
    public function _pageFilter(&$params){
        $params['fields'] = ['view'];
        $params['data'] = [
            'view'=>['admin','admin_access']
        ];
    }
    /**
     * 变更密码
     * @return mixed
     */
    public function changepwd(){
        if($this->request->isAjax()){
            try{
                $newPassword = $this->request->param('new_password');
                $oldPassword = $this->request->param('old_password');
                if(empty($newPassword) || empty($oldPassword)){
                    exception('请输入新旧密码！');
                }
                $this->model->changepwd($this->loginInfo['id'],$newPassword,$oldPassword);
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('操作成功！',url('Public/login'));
        }
        return $this->fetch();
    }
    /**
     * 退出登录
     */
    public function loginOut(){
        try{
            $this->model->loginOut();
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('退出成功！',url('public/login'));
    }
}
